import OrderModel from "../../../../models/Order";

const orderModel = new OrderModel()

Component({
  properties: {},
  data: {
    list: [
      { image: 'fav', text: '收藏', url: 'fav' },
      { image: 'message', text: '消息公告', url: 'news' },
      { image: 'comment', text: '我的评论', url: 'comment' },
      { image: 'about', text: '关于我们', url: 'about' },
      { image: 'service', text: '联系客服', url: 'service' },
      // { image: 'material', text: '素材库', url: 'material' },
      // { image: 'clear', text: '清除缓存', url: '/cache/true' },
      { image: 'logout', text: '退出登录', url: 'logout' },
      { image: 'clear', text: '门店核销', url: 'scan' }
    ]
  },
  methods: {
    handleClick(event) {
      const item = event.currentTarget.dataset.item
      const url = item.url
      if (url === 'logout') {
        wx.showModal({
          title: '确定退出登录吗?',
          success: res => {
            if (res.confirm) {
              wx.clearStorageSync()
              wx.switchTab({
                url: '/pages/home/index'
              })
            }
          }
        })
      } else if (url === 'scan') {
        // wx.showModal({
        //   title: '确定核销吗?',
        //   success: res => {
        //     if (res.confirm) {
        //       wx.scanCode({
        //         success (res) {
        //           console.log(res)
        //         }
        //       })
        //     }
        //   }
        // })
        wx.scanCode({
          // 只让扫二维码
          scanType: ['qrCode'],
          success (res) {
            const { result } = res
            // 可以扫出二维码的内容
            // 如果是小程序, 则显示为"*"
            console.log(res)
            orderModel.scanQrcode({ item_id: result }).then(() => {
              wx.showToast({
                title: '操作成功',
                icon: 'success',
                duration: 1000
              })
            })
          },
          fail() {
            wx.showToast({
              title: '错误',
              icon: 'error',
              duration: 1000
            })
          }
        })
      }else {
        wx.navigateTo({
          url: `/sub_common/pages/${url}/index`
        })
      }
    }
  }
});
